﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace EpiNEXT.WPF
{
    public class CheckBoxList : OrientableListSelector
    {
        protected override DependencyObject GetContainerForItemOverride()
        {
            return new CheckBoxListBoxItem();
        }

        public class CheckBoxListBoxItem : ListBoxItem
        {
            public CheckBoxListBoxItem()
            {
                object resource = this.TryFindResource("RadioButtonListBoxItemStyle");
                if (resource != null)
                {
                    this.Style = resource as Style;
                }
            }

            protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
            {
                DependencyObject dep = (DependencyObject)e.OriginalSource;

                while ((dep != null) && !(dep is ListBoxItem))
                {
                    dep = VisualTreeHelper.GetParent(dep);
                }

                if (dep == null)
                    return;

                ListBoxItem item = (ListBoxItem)dep;
                if (item.IsSelected)
                {
                    item.IsSelected = !item.IsSelected;
                    e.Handled = true;
                }
                base.OnMouseLeftButtonDown(e);
            }
        }  
    }
}
